package com.aloha.boot01.controller;

import com.aloha.boot01.service.lintener.ObjectEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author zhdai
 * @date 2023/10/27 10:49
 */
@RestController
public class ListenerController {
    @Resource
    private ApplicationContext publisher;

    private static final ExecutorService POOL = Executors.newFixedThreadPool(5);

    @EventListener(ObjectEvent.class)
    public void listener(ObjectEvent event) {
        POOL.submit(()->{
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("-----------------监听器处理-------------------");
        });
    }

    @GetMapping("/publish")
    public String send() {
        publisher.publishEvent(new ObjectEvent());
        return "已发送";
    }
}
